<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="handleOk"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="handleOk" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="角色编码"
            prop="roleCode"
            hasFeedback
          >
            <a-input
              placeholder="请输入角色编码"
              v-model.trim="model.roleCode"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="角色名称"
            prop="name"
            hasFeedback
          >
            <a-input placeholder="请输入角色名称" v-model.trim="model.name" />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="角色描述"
            hasFeedback
          >
            <a-input
              placeholder="请输入角色描述"
              v-model.trim="model.roleDescription"
            />
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
export default {
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
    };
  },
  computed: {
    rules() {
      return {
        roleCode: [{ required: true, message: "请输入角色编码" }],
        name: [{ required: true, message: "请输入角色名称" }],
      };
    },
  },
  methods: {
    // 新增
    async modalAdd() {
      this.modalEdit();
    },
    // 编辑
    async modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.model = Object.assign({}, record);
    },
    async submitVulFun() {
      this.confirmLoading = true;
      let res;
      if (!this.model.id) {
        res = await api.RoleAdd(this.model);
      } else {
        res = await api.RoleEdit(this.model);
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.confirmLoading = false;
      this.$message.success(res.message);
      this.$emit("ok");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.submitVulFun();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
  },
};
</script>


